<template>
  <div class="drowdown" ref="dropdownRef">
    <a href="#" @click.prevent="toggleOpen" class="btn btn-outline-light my-2 dropdown-toggle">{{title}}</a>
  <ul class="dropdown-menu" v-show="isOpen" :style="{display: 'block'}">
    <slot></slot>
  </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, onUnmounted, watch } from 'vue'
import useClickOutSide from '../hooks/useClickOutSide'
export default defineComponent({
  name: 'Dropdown',
  props: {
    title: {
      type: String,
      required: true
    }
  },
  setup () {
    const isOpen = ref(false)
    const dropdownRef = ref<any>(null)
    const toggleOpen = () => {
      isOpen.value = !isOpen.value
    }

    const isClickOutside = useClickOutSide(dropdownRef)
    watch(isClickOutside, () => {
      if (isOpen.value && isClickOutside.value) {
        isOpen.value = false
      }
    })
    return {
      isOpen,
      toggleOpen,
      dropdownRef
    }
  }
})
</script>

<style>

</style>
